//
//  ViewController.m
//  download
//
//  Created by lanou3g on 15/10/16.
//  Copyright © 2015年 lanou3g. All rights reserved.
//
#import "DownloadViewController.h"
#import "download.h"

@interface DownloadViewController ()

@property(nonatomic,strong)UIProgressView *progress;

@property(nonatomic,strong)download *downlo;

@property(nonatomic,strong)UILabel *label;

@end

@implementation DownloadViewController



+(instancetype)downloadViewController{
    return [self alertControllerWithTitle:@"下载进度" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
}




-(instancetype)init{
    if (self=[super init]) { 
        [self setViews];
    }
      return self;
}



- (void)setViews{
    [super viewDidLoad];
   
    _downlo=[[download alloc]init];
    
    _label =[[UILabel alloc]initWithFrame:CGRectMake(240, 20, 150, 50)];
    _label.textColor=[UIColor blackColor];
    [self.view addSubview:_label];

    
    _progress =[[UIProgressView alloc]initWithFrame:CGRectMake(30, 45, 200, 50)];
    _progress.progressViewStyle=UIProgressViewStyleDefault;
    _progress.progressTintColor=[UIColor blackColor];
    _progress.trackTintColor=[UIColor yellowColor];
    _progress.progressImage=[UIImage imageNamed:@"AirPic_toolbaritem_bg@2x.png"];
    _progress.trackImage=[UIImage imageNamed:@"aio_validate_split.png"];
    //设置高度
    _progress.transform=CGAffineTransformMakeScale(1.0f, 5.0f);
    self.view.backgroundColor=[UIColor whiteColor];
   [self.view addSubview:_progress];
    UIAlertAction *action2=[UIAlertAction actionWithTitle:@"停止" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [_downlo pauseOperation];
        
    }];
    UIAlertAction *action1=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [_downlo cancelOperation];
       // [self presentAlertController];
    }];
    
    [self addAction:action2];
    [self addAction:action1];
}


-(void)presentAlertController{
    UIAlertController *cancelController=[UIAlertController alertControllerWithTitle:@"下载失败" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:cancelController animated:YES completion:nil];
    sleep(3);
    [self dismissViewControllerAnimated:cancelController completion:nil];
}



-(void)testProgress:(CGFloat)progre{
    [_progress setProgress:progre animated:YES];
    NSString *prop=[NSString stringWithFormat:@"%f",progre*100];
    _label.text=[prop stringByAppendingString:@"%"];
    if (progre==1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}



-(void)testDownLoad:(NSString *)url{
    NSString *Path =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *savePath=[Path stringByAppendingPathComponent: [NSString stringWithFormat:@"%@",[url substringFromIndex:45]]];
    NSLog(@"路径:%@",savePath);
    [_downlo downloadFileWithOption:nil withURL:url savePath:savePath downloadSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
    } downloadFailure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败:%@",error);
    } progress:^(float progress) {
        [self testProgress:progress];
    }];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
